﻿using System;
using System.Web;
using DevMaker.SIC.Dao.Business;
using DevMaker.SIC.Model;
using log4net;


namespace WebApplication1 {
    public partial class CierreReporte : BasePage {
        readonly TurnoBussiness _turnoBussiness = new TurnoBussiness();
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public override bool SafePage {
            get { return true; }
        }

        public override string TemplateName {
            get { return "CierreReporte"; }
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "") {
                var turno = _turnoBussiness.ObtieneTurnoPorId(Convert.ToInt32(Request.QueryString["id"]));
                AttributesTemplate.Add("turno", turno);
                var tableHTML = turno.GenereteHTMLTableForReporteCierre();
                turno.Observaciones = new ObservacionBussiness().ObtienePorTurno(turno);
                foreach (var o in turno.Observaciones) {
                    o.Comentario = HttpUtility.HtmlEncode(o.Comentario);
                    o.Comentario = o.Comentario.Replace(Environment.NewLine, "<br/>");
                }
                AttributesTemplate.Add("tableHTML", tableHTML);

            }

            if (Request.QueryString["from"] == "cierre") {
                AttributesTemplate.Add("mostrarExito", true);
            }

            if (Request.Form["idTurno"] != null && Request.Form["idTurno"] != "") {
                if (AttributesTemplate.ContainsKey("turno")) {
                    AttributesTemplate.Remove("turno");
                }

                var turno = _turnoBussiness.ObtieneTurnoPorId(Convert.ToInt32(Request.Form["idTurno"]));
                AttributesTemplate.Add("turno", turno);
                turno.Sucursal = Session["sucursal"] as Sucursal;
                var tableHTML = turno.GenereteHTMLTableForReporteCierre();
                if (AttributesTemplate.ContainsKey("tableHTML")) {
                    AttributesTemplate.Remove("tableHTML");
                }
                AttributesTemplate.Add("tableHTML", tableHTML);
                if (Request.Form["nuevoComentario"] != null) {
                    var observacion = new Observacion();
                    observacion.Comentario = Request.Form["comentario"];
                    observacion.Usuario = Session["usuarioConectado"] as Usuario;
                    new ObservacionBussiness().Crear(turno, observacion);
                }
                turno.Observaciones = new ObservacionBussiness().ObtienePorTurno(turno);
                foreach (var o in turno.Observaciones) {
                    o.Comentario = HttpUtility.HtmlEncode(o.Comentario);
                    o.Comentario = o.Comentario.Replace(Environment.NewLine, "<br/>");
                }
            }
        }
    }
}
